const Tesseract = require('tesseract.js');
const path = require('path');
const { PNG } = require('pngjs');
const fs = require("fs");

async function test1() {
    const imagePath = path.join(__dirname, 'img/page-mid.png');
    const t1Buffer = fs.readFileSync(imagePath);

    const t1Png = PNG.sync.read(t1Buffer);

    const height=t1Png.height
    const width=t1Png.width
    const colorArr=['255,0,0','255,0,1','255,0,2','0,128,0','0,128,1','0,128,2']
    const tipArr=['抄底,多','急涨,多','冲底,多','空顶,空','急跌,空','冲顶,空']
    const signal={
        index:-1,
        color:'',
        tip:''
    }
    for (let x = width-1; x >=0; x--) {
        for (let y = 0; y < height; y++) {
            const pos = y * width + x;
            const imgPos = pos * 4;
            const r=t1Png.data[imgPos]
            const g=t1Png.data[imgPos + 1]
            const b=t1Png.data[imgPos + 2]
            const key=[r,g,b].join(",")
            const index=colorArr.includes(key)
            if(index>-1){
                signal.index=index
                signal.color=key
                signal.tip=tipArr[index]
                break
            }
        }
    }

    if(colorArr.includes('255,0,0')){
        signal.tip='抄底,多'
    }
    console.log('抄底',colorArr.includes('255,0,0'))
    console.log('急涨',colorArr.includes('255,0,1'))
    console.log('冲底',colorArr.includes('255,0,2'))

    console.log('空顶',colorArr.includes('0,128,0'))
    console.log('急跌',colorArr.includes('0,128,1'))
    console.log('冲顶',colorArr.includes('0,128,2'))
    return colorArr
}
test1()
